import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
 
const service =axios.create({
    baseURL:'/api',
    timeout:9000
})

const err=(error)=>{
    if(error.response){
        let data=error.response.data
        const token=Vue.local.get("ACCESS_TOKEN")
        switch(error.response.status){
            case 403:
                Vue.prototype.$notification.error({message:'系统提示',description:'拒绝访问',duration:4})
                break
            case 500:
                let type=error.response.request.responseType
                if(type==='blob'){
                    blobToJson(data)
                    break
                }
                if(token&&data.message.includes("token失效")){
                    Vue.prototype.$Modal.error({
                        title:'登录已过期',
                        content:'很抱歉，登录已过期，请重新登录',
                        okText:'重新登陆',
                        mask:true,
                        onOk:()=>{
                            store.dispatch('Logout').then(()=>{
                                Vue.local.remove("ACCESS_TOKEN")
                                try{
                                    let path=window.document.location.pathname
                                    if(path!='/'&& path.indexOf('/user/login')==-1){
                                        window.location.reload()
                                    }
                                }catch(e){
                                    window.location.reload()
                                }
                            })
                        }
                    })
                }
                break
            case 404:
                Vue.prototype.$notification.error({message:'系统提示',description:'很抱歉,资源未找到！',duration:4})
                break
            case 504:
                Vue.prototype.$notification.error({ message: '系统提示', description: '网络超时'})
                break
            case 401:
                Vue.prototype.$notification.error({ message: '系统提示', description:'未授权，请重新登录',duration: 4})
                    if (token) {
                      store.dispatch('Logout').then(() => {
                        setTimeout(() => {
                          window.location.reload()
                        }, 1500)
                      })
                    }
                    break
            default:
                Vue.prototype.$notification.error({
                    message:"系统提示",
                    description:data.message,
                    duration:4
                })
                break
        }
    }else if(error.message){
        if(error.message.includes('timeout')){
            Vue.prototype.$notification.error({message:'系统提示',description:'网络延时'})
        }else {
            Vue.prototype.$notification.error({message:'系统提示',description:error.message})
        }
    }
    return Promise.reject(error)
}

service.interceptors.request.use(config=>{
    const token=Vue.local.get("ACCESS_TOKEN")
    if(token){
        config.headers[ 'Authorization' ] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
    }
    return config;
},(error)=>{
    return Promise.reject(error)
})

service.interceptors.response.use((response)=>{
    return response.data
},err)

function blobToJson(data){
    let fileReader=new FileReader()
    let token=Vue.local.get("ACCESS_TOKEN")
    fileReader.onload=function(){
        try{
            let jsonData=JSON.parse(this.result)
            if(jsonData.status===500){
                if(token&&jsonData.message.includes("token失效")){
                    Vue.prototype.$Modal.error({
                        title:'登录已过期',
                        content:'很抱歉，登录已过期，请重新登录',
                        okText:'重新登陆',
                        mask:true,
                        onOk:()=>{
                            store.dispatch('Logout').then(()=>{
                                Vue.local.remove("ACCESS_TOKEN")
                                try{
                                    let path=window.document.location.pathname
                                    if(path!='/'&& path.indexOf('/user/login')==-1){
                                        window.location.reload()
                                    }
                                }catch(e){
                                    window.location.reload()
                                }
                            })
                        }
                    })
                }
            }
        }catch(err){
            console.log("blob解析fileReader返回err",err)
        }
    }
    fileReader.readAsText(data)
}

export default service